package mn.buya;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable.Callback;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class DrawCanvas extends SurfaceView implements Callback,
		android.view.SurfaceHolder.Callback {

	private CanvasThread canvasThread;

	public DrawCanvas(Context context) {
		super(context);
	}

	public DrawCanvas(Context context, AttributeSet attrs) {
		super(context, attrs);

		this.getHolder().addCallback(this);
		this.canvasThread = new CanvasThread(getHolder());
		this.setFocusable(true);
	}

	public DrawCanvas(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void startDrawImage() {
		canvasThread.setRunning(true);
		canvasThread.start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		canvasThread.setRunning(false);
		while (retry) {
			try {
				canvasThread.join();
				retry = false;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onDraw(Canvas c) {
//		Bitmap sweet = BitmapFactory.decodeResource(getResources(),
//				R.drawable.back10);
//		canvas.drawColor(color.black);
//
//		canvas.drawBitmap(sweet, 0, 0, null);
//		canvas.translate(Shared.fAnimWordX, Shared.fAnimWordY);
//
//		Paint paint = new Paint();
//		paint.setStrokeWidth(15);
//		paint.setTextSize(30);
//		paint.setStyle(Style.FILL);
//		paint.setColor(Color.BLACK);
//		// canvas.drawText(Shared.sAnimWord, Shared.fAnimWordX,
//		// Shared.fAnimWordY, paint);
//		if (Shared.fAnimWordY > 20) {
//			if (Shared.isAnim) {
//				Shared.fAnimWordY -= 6;
//			}
//		}
//		paint.setColor(Color.RED);
//		canvas.drawText(Shared.sAnimWord, Shared.fAnimWordX, Shared.fAnimWordY,
//				paint);
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);

		// make the entire canvas white
		paint.setColor(Color.WHITE);
		c.drawPaint(paint);
		
		int x = 75; // x increases from left to right
		int y = 140; // y increases from top to bottom

		paint.setColor(Color.GRAY);
		paint.setTextSize(25);
		String str2rotate = "Rotated text!";

		// draw bounding rect before rotating text
		Rect rect = new Rect();

		// rotate the canvas on center of the text to draw
		c.rotate(-45, x + rect.exactCenterX(), y + rect.exactCenterY());

		// draw the rotated text
		paint.setStyle(Paint.Style.FILL);
		c.drawText(str2rotate, x, y, paint);

		//undo the rotate
		c.restore();
		System.out.println("Drawing canvas!");
		
	}

	private class CanvasThread extends Thread {
		private SurfaceHolder surfaceHolder;
		private boolean isRun = false;

		public CanvasThread(SurfaceHolder holder) {
			this.surfaceHolder = holder;
		}

		public void setRunning(boolean run) {
			this.isRun = run;
		}

		@Override
		public void run() {
			Canvas c;

			while (isRun) {
				c = null;
				try {
					c = this.surfaceHolder.lockCanvas(null);
					synchronized (this.surfaceHolder) {
						try {
							DrawCanvas.this.onDraw(c);
						} catch (Exception e) {
							System.out.println("Анхаар! : " + e.toString());
						}
					}
				} finally {
					try {
						surfaceHolder.unlockCanvasAndPost(c);
					} catch (Exception e) {
						System.out.println("Анхаар! : " + e.toString());
					}
				}
			}
		}
	}
}